<?php
/**
 * @version 1.5
 * @package RealEstateManagerTop
 * @copyright 2009 OrdaSoft
 * @author 2009 Sergey Drughinin-OrdaSoft(Sergey.dru@gmail.com)
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
 * @description RealEstateManagerTop for RealEstateManager Component
*/

/** ensure this file is being included by a parent file */
defined( '_JEXEC' ) or die( 'Restricted access' );

global $mosConfig_live_site, $mosConfig_absolute_path;
$doc =& JFactory::getDocument(); 
$doc->addStyleSheet($mosConfig_live_site.'/administrator/components/com_realestatemanager/includes/realestatemanager.css');

$database = &JFactory::getDBO();
if ( !function_exists('getWhereUsergroupsString')) {
  function getWhereUsergroupsString( $table_alias ) { 
    global $my;
     
    if ( isset($my->id) AND $my->id != 0 ) { 
     
      $usergroups_sh = getGroupsByUser ($my->id,'');       
  
      //$usergroups_sh = '-2'.$usergroups_sh;   
      
    } else {
       $usergroups_sh = array ();
    }
    $usergroups_sh[] = -2; 
      
          
    $s = '';
    for ($i=0; $i<count($usergroups_sh); $i++) {  
      $g = $usergroups_sh[$i];
      $s .= " $table_alias.params LIKE '%,{$g}' or $table_alias.params = '{$g}' or $table_alias.params LIKE '{$g},%' or $table_alias.params LIKE '%,{$g},%' ";
      if ( ($i+1)<count($usergroups_sh) )
        $s .= ' or ';
    } 
    return $s;
  } 
}

if( !function_exists( 'sefreltoabs')) {
  function sefRelToAbs( $value ) {
    //Need check!!!

    // Replace all &amp; with & as the router doesn't understand &amp;
    $url = str_replace('&amp;', '&', $value);
    if(substr(strtolower($url),0,9) != "index.php") return $url;
    $uri    = JURI::getInstance();
    $prefix = $uri->toString(array('scheme', 'host', 'port'));
    return $prefix.JRoute::_($url);
  }
}

//Common parameters
$sort_top_by    = $params->def('sort_by_top', 0);        //Get how to sort the top items
$show_published    = $params->def('only_published', 1);    //Get if we only show published items
$show_covers     = $params->def('covers', 0 );             //Get if we Real Estate  photos
$cover_height    = $params->def('cover_height', "50");     //Get Real Estate  photos
$show_extra        = $params->def('extras', 1 );             //Get if we show second column with additional info

$show_ranking    = $params->def('ranking', 0 );             //Get if we show the ranking next to them
//Individual parameters
$count_realestate = intval($params->def('books',1)); 
$moduleclass_sfx = $params->get('moduleclass_sfx', '');
$ItemId_tmp_from_params=$params->get('ItemId');

//realestate
//Definition of Sorts
switch($sort_top_by) 
{
    case 0:
        $sql_sort_top = "hits";
        break;
    case 1:
        $sql_sort_top = "date";
        break;
   // case 2:
     //   $sql_sort_top = "rating";
   //     break;
}

 
//Check if only display published items
if ($show_published==1) {
    $sql_published = "AND h.published=1";
} else {
    $sql_published = "";
}
$s=GetWhereUserGroupsString("c");

if($count_realestate!='' && $count_realestate!=0) {
$selectstring = "SELECT h.htitle,h.id,h.image_link,h.hits,hc.idcat 
		\nFROM #__rem_houses AS h
		\nLEFT JOIN #__rem_categories AS hc ON hc.iditem=h.id
		\nLEFT JOIN #__rem_main_categories AS c ON c.id=hc.idcat
		\nWHERE ($s) ".$sql_published." ORDER BY ".$sql_sort_top." DESC LIMIT 0$count_realestate;";
    
    $database->setQuery($selectstring);
    $rows_realestate= $database->loadObjectList();
	//print_r($database);
}
    $selectstring = "SELECT id  FROM #__menu WHERE menutype like '%menu%' AND link LIKE '%option=com_realestatemanager%' AND params LIKE '%back_button%'";
    $database->setQuery($selectstring);
    $ItemId_tmp_from_db = $database->loadResult();
    if($ItemId_tmp_from_params==""){
      $ItemId_tmp=$ItemId_tmp_from_db;
    }
    else{
      $ItemId_tmp=$ItemId_tmp_from_params;
    }
    

function Display_REM_top($rows, $name, $show_ranking, $show_covers, $show_extra, $cover_height, $ItemId_tmp, $moduleclass_sfx ) {
        $rank_count = 0;
        $span=0;
        if($show_ranking!=0) $span++;
        if($show_covers!=0) $span++;
        if($show_extra!=0) $span++;
    ?>   
        <div class="realestate_<?php if($moduleclass_sfx!='') echo $moduleclass_sfx;?>">
         <table cellpadding="0" cellspacing="0" class="basictable" width="100%" border="0">          
         
    <?php foreach ($rows as $row) {
    $rank_count = $rank_count + 1;    //start ranking
    $link1 ="index.php?option=com_realestatemanager&amp;task=view&amp;id=".$row->id."&amp;catid=".$row->idcat."&amp;Itemid=".$ItemId_tmp."";
$mosConfig_absolute_path=JPATH_BASE;
$mosConfig_live_site=JURI::base(true);
       $imageURL = $row->image_link ;
        if($imageURL!=''){  
            $file_pth= pathinfo($imageURL);
            $file_type=".".$file_pth['extension'];
            if(array_key_exists  ( 'filename' , $file_pth  ) ) $file_name=$file_pth['filename'];
            else $file_name = substr($imageURL, 0,strlen($imageURL)-strlen($file_pth['extension'] ) -1 );
            $file=$mosConfig_absolute_path . '/components/com_realestatemanager/photos/'. $file_name ."_gallery". $file_type;
            if(!file_exists($file)){
              $file=$mosConfig_live_site . '/components/com_realestatemanager/photos/'. $file_name."_mini".$file_type;
            }
            else $file=$mosConfig_live_site . '/components/com_realestatemanager/photos/'. $file_name."_gallery".$file_type;
            $imageURL=$file;
        }
        else $imageURL = "./components/com_realestatemanager/images/no-img_eng.gif";


?>
        <tr>
            <?php if($show_ranking==1) { echo "<td>".$rank_count.":&nbsp;</td>"; } //Add Column for Ranking if param set ?>
            <?php if ($show_covers==1) {?>
            <td>
            <a href="<?php echo $link1; ?>" > <img src="<?php echo $imageURL; ?>" alt="<?php echo $row->htitle; ?>" hspace="2" vspace="2" border="0" height="<?php echo $cover_height; ?>" /></a>
            </td><?php 
                } ?>
            <td width="160" <?php if($span!=0)  echo "colspan='$span'";?>>
                <a href="<?php echo sefRelToAbs( $link1 ); ?>" target="_self""><?php echo $row->htitle; ?></a>
            </td>
            <?php 
			if($show_extra==1) {?>
            <td align="right">
                     <font class='small'>(<?php echo $row->hits; ?>)</font>
            </td>
        </tr>
           <?php } ?>
<?php } ?>
        </table>
<?php
}    
?>

   
<?php 
if(count($rows_realestate)) { Display_REM_top($rows_realestate, "Top  Real Estate", $show_ranking, $show_covers, $show_extra,$cover_height, $ItemId_tmp, $moduleclass_sfx); }
   
?>
<div style="text-align: center;"><a href="http://ordasoft.com" style="font-size: 10px;">Powered by OrdaSoft!</a></div>
</div>
     
     
     
     
  
